<?php
/**
* PrayInfo module
* Show Prayonline Info
* For PrayOnline of Hoithanh.com
* 
* @author: trungdt
* @since: Feb 13 2011
* @version: 1.0
* 
* Change logs
* Feb 13, 2011 - trungdt - First Create
*/

class prayinfomodule {
	function name() { return "PrayOnline Info";  }
	function description() { return "Show demands and witness in prayonline system";  }
	
	function author() { return "Trungdt - ABSoft - http://absoft.vn "; }
	
	function hasContent() { return false; }
	function hasSources() { return false; }
	function hasViews()   { return true; }
	
	function supportsWorkflow() { return false; }
	
	function permissions($internal = "") {
		return array("configure"=>"Configure");
	}
	
    function getLocationHierarchy($loc) {
        // because this module does not belong to any page, we fake source to get permission
        if ($loc->src=="")
        {
          $loc->src="@prayinfomodule"; 
        }
        if ($loc->int == '') return array($loc);
        else return array($loc,exponent_core_makelocation($loc->mod,$loc->src));
    }
    
	function show($view,$loc,$title = '') {
		global $user, $db;
        
        $max_page=5;
        $item_per_page=5;
        
        if (!isset($user->time_belt))
        {
            $user->time_belt=0;
        }
        
        // get config
        $temp_loc=serialize($loc);
        $config=$db->selectObject("prayinfomodule_config","location_data = '{$temp_loc}'");
        if (isset($config->id)){
            $max_page=$config->pages;
        }
        $max_item=$max_page*$item_per_page;
        
        // first we count all demands and witness
        $demands=$db->selectObjects("demands","status=2 ORDER BY publish_date DESC, post_date DESC, important_level DESC LIMIT 0,{$max_item}");
        
        // for witness
        $witness=$db->selectObjects("witness","status=2 ORDER BY  publish_date DESC, post_date  DESC LIMIT 0,{$max_item}");
        
        // page spilt for demands
        $obj_page=array();
        $demand_obj=array();
        foreach ($demands as $k=>$v)
        {
            $v->seo_title=($v->id."-"._to_vietnamese_with_nomark($v->name));
            $v->name=_ab_safe_html_string($v->name);
            $v->content=_ab_safe_html_string($v->content); 
            $v->date_time=date("d/m/Y \- h:i:s",$v->post_date);
           if (count($obj_page)==$item_per_page)
           {
               $demand_obj[]=$obj_page;
               $obj_page=array();   
           }
           
           if (count($obj_page)<$item_per_page)
           {
               $obj_page[]=$v;
           } 
        }
        if ($obj_page!=null)
            $demand_obj[]=$obj_page;
        
        
        // page split for witness
        $obj_page=array();
        $witness_obj=array();
        foreach ($witness as $k=>$v)
        {
           $v->seo_title=($v->id."-"._to_vietnamese_with_nomark($v->name));
           $v->name=_ab_safe_html_string($v->name);
           $v->content=_ab_safe_html_string($v->content);
           $v->post_date=$v->post_date-(7+$user->time_belt)*3600; // From GMT +7 to local time
           $v->date_time=date("d/m/Y \- h:i:s",$v->post_date);
           if (count($obj_page)<$item_per_page)
           {
               $obj_page[]=$v;
           }
           else
           {
               $witness_obj[]=$obj_page;
               $obj_page=array();
           } 
        }
        if ($obj_page!=null)
            $witness_obj[]=$obj_page;

        $template = new template('prayinfomodule',$view,$loc);
        $template->assign("demands",$demand_obj);
        $template->assign("witness",$witness_obj);
		$template->register_permissions(array(),$loc);
		$template->output($view);
	}
}

?>
